//
//  PingYinVC.swift
//  StudyChinese
//
//  Created by Wenzhe Fan on 2019/6/19.
//  Copyright © 2019年 Wenzhe Fan. All rights reserved.
//

import UIKit
import AVFoundation
import CoreGraphics
class PingYinVC: UIViewController{
    @IBOutlet weak var mycolletionView: UICollectionView!
    var sectionTitileArr:[String] = ["声母表","韵母表","整体认读音节"]
    
    var lookArrA:[String] = ["b","p","m","f","d","t","n","l",
                             "g","k","h","j","q","x","zh","ch",
                             "sh","r","z","c","s","y","w"]
    var lookArrB:[String] = ["a","o","e","i","u","ü","ai","ei",
                             "ui","ao","ou","iu","ie","üe","er","an",
                             "en","in","un","ün","ang","eng","ing","ong"]
    var lookArrC:[String] = ["zhi","chi","shi","ri","zi","ci","si","yi","wu","yu","ye","yue","yuan","yin","yun","ying"]
    
    var readArrA:[String] = ["波","迫     ","磨","佛","德","特","讷","了","戈","可","河","鸡","七","喜","支","齿","狮","日","字","兹","丝","衣","乌"]
    var readArrB:[String] = ["啊","喔","鹅","衣","乌","迂","哀","诶",
                             "胃","熬","欧","you","夜","月","耳","安",
                             "恩","音","韵","晕","昂","鞥","鹰","轰"]
    var readArrC:[String] = ["支","池","食","日","字","词","思","义",
                             "乌","鱼","页","月","元","音","晕","鹰"]
    @IBOutlet weak var imgaeView: UIImageView!
    override func viewDidLoad() {
        super.viewDidLoad()

     
        
    }
    
    @IBAction func tap(_ sender: UITapGestureRecognizer) {
        
        let xunit = self.imgaeView.frame.size.width  / 10
        debugPrint(xunit)
        let yunit = self.imgaeView.frame.height / 13
        debugPrint(yunit)
        
       
        let point:CGPoint = sender.location(in: self.imgaeView)
        
        let index_X =  point.x / xunit
        
        let index_Y = point.y / yunit
        
        
        
        debugPrint(index_X,index_Y)
        
//       self.startRead()
    }
    @IBAction func readBtn(_ sender: UIButton) {
        startRead(readstring: readArrA[sender.tag] as! String)
    }
    
    /// 读事件
    /// - Parameter readstring: 读事件
   func startRead(readstring:String = "波"){
        let avspeach = AVSpeechSynthesizer.init()
        let utterance = AVSpeechUtterance.init(string:readstring)
        utterance.rate = 0
        avspeach.speak(utterance)
    }
}
extension PingYinVC:UICollectionViewDelegate,UICollectionViewDataSource{
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 3
    }
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        switch section {
        case 0:
            return 23
        case 1:
            return 24
        default:
            return 16
        }
    }
    func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
        var reusabaleView:HeaderCollectionReusableView!
        if kind == UICollectionView.elementKindSectionHeader {
            reusabaleView = collectionView.dequeueReusableSupplementaryView(ofKind:UICollectionView.elementKindSectionHeader, withReuseIdentifier: "HeaderCollectionReusableView", for: indexPath) as? HeaderCollectionReusableView
            reusabaleView.titleNameLB.text = self.sectionTitileArr[indexPath.section] as String
        }
        if kind == UICollectionView.elementKindSectionFooter{
            
        }
        return reusabaleView
    }
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "PinyinCell", for: indexPath) as! PinyinCell
        
        switch indexPath.section {
        case 0:
            cell.pinyinLB.text = "\(self.lookArrA[indexPath.row])"
            break
        case 1:
            cell.pinyinLB.text = "\(self.lookArrB[indexPath.row])"
            break
        case 2:
            cell.pinyinLB.text = "\(self.lookArrC[indexPath.row])"
        default:
            break
        }
        
        
        return cell
    }
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
       
//        self.startRead()
        
        switch indexPath.section {
        case 0:
            self.startRead(readstring: self.readArrA[indexPath.row])

            break
        case 1:
            self.startRead(readstring: self.readArrB[indexPath.row])
            break
        case 2:
            self.startRead(readstring: self.readArrC[indexPath.row])
            break
        default:
            break
        }
    }
    
}

